package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.UserEvaluateBO;
import com.junzhiit.nengji.bo.UserEvaluateCountBO;
import com.junzhiit.nengji.cms.service.UserEvaluateService;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.cms.web.vo.UserEvaluateFindVO;
import com.junzhiit.nengji.cms.web.vo.UserEvaluateVO;
import com.junzhiit.nengji.dao.OrdersDAO;
import com.junzhiit.nengji.dao.OrdersGoodsDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.UserEvaluateDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Orders;
import com.junzhiit.nengji.model.OrdersGoods;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.UserEvaluate;
import com.junzhiit.nengji.model.enums.OrderGoodsStatus;
import com.junzhiit.nengji.model.enums.OrderStatus;
import com.junzhiit.nengji.model.enums.Status;

@Service
public class UserEvaluateServiceImpl extends BaseServiceImpl implements UserEvaluateService {

	@Autowired
	private UserEvaluateDAO userEvaluateDAO;
	
	@Autowired
	private OrdersGoodsDAO ordersGoodsDAO;

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private OrdersDAO ordersDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, UserEvaluateFindVO userEvaluateFindVO) {

		UserEvaluate userEvaluate = new UserEvaluate();
		BeanUtils.copyProperties(userEvaluateFindVO, userEvaluate);
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(userEvaluateFindVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		userEvaluate.setShopId(shop.getId());

		List<UserEvaluateBO> userEvaluateBOs = userEvaluateDAO.findAll(userEvaluate, userEvaluateFindVO.getScoreState(),
				dtp.getStart(), dtp.getLength());

		List<UserEvaluateVO> list = new ArrayList<UserEvaluateVO>();
		for (UserEvaluateBO userEvaluateBO : userEvaluateBOs) {
			UserEvaluateVO userEvaluateVO = new UserEvaluateVO();
			List<String> images=new ArrayList<String>();
			String image = userEvaluateBO.getImage();
			if(StringUtils.isNotBlank(image)){
				String[] imageArr = image.split("\\,");
				images = Arrays.asList(imageArr);
			}

			String standards = userEvaluateBO.getStandards();
			if (StringUtils.isNotBlank(standards)) {
				standards = standards.replaceAll("\\+", " ");
				userEvaluateBO.setStandards(standards);
			}
			
			BeanUtils.copyProperties(userEvaluateBO, userEvaluateVO);
			userEvaluateVO.setImages(images);
			list.add(userEvaluateVO);
		}

		int totalCount = userEvaluateDAO.getTotalCount(userEvaluate, userEvaluateFindVO.getScoreState());
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO findEvaluateCount(String goodsId) {
		ResultVO result = new ResultVO();
		UserEvaluateCountBO userEvaluateCountBO = userEvaluateDAO.findEvaluateCount(goodsId);
		result.setData(userEvaluateCountBO);
		return result;
	}
	
	@Override
	public ResultVO getShopEvaluateScore(UserEvaluateFindVO userEvaluateFindVO){
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(userEvaluateFindVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		Double score=userEvaluateDAO.getShopEvaluateScore(shop.getId());
		result.setData(score);
		return result;
	}
	

	@Override
	@Transactional
	public ResultVO defaultEvaluate() {
		ResultVO result=new ResultVO();
		
		//获取七天前的日期字符串 "yyyy-MM-dd"格式
		Date date=DateUtil.addDays(new Date(), -7);
		
		List<UserEvaluate> list= ordersDAO.getDefaultEvaluate(date);
		List<OrdersGoods> ordersGoodslist= new ArrayList<OrdersGoods>();
		for (UserEvaluate userEvaluate : list) {
			OrdersGoods ordersGoods=new OrdersGoods();
			ordersGoods.setId(userEvaluate.getOrdersGoodsId());
			ordersGoods.setStatus(OrderGoodsStatus.ESTIMATE.toIntValue());
			ordersGoods.setUpdatedTime(new Date());
			ordersGoodslist.add(ordersGoods);
			
			userEvaluate.setScore(5d);
			userEvaluate.setContent("好评");
			userEvaluate.setCreatedTime(new Date());
			userEvaluate.setStatus(Status.ACTIVE.toIntStatus());
		}
		ordersGoodsDAO.batchUpdate(ordersGoodslist);
		result.setData(userEvaluateDAO.batchInsert(list));
		
		List<Orders> listOrders=ordersDAO.getOrdersDefaultEvaluate(date);
		for (Orders orders : listOrders) {
			orders.setStatus(OrderStatus.COMPLETE.toIntValue());
			orders.setUpdatedTime(new Date());
			orders.setCompleteTime(new Date());
		}
		ordersDAO.batchUpdate(listOrders);
		
		return result;
	}
	

}
